define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'car/image/index' + location.search,
                    table: 'car_images',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: true,
                visible: true,
                showToggle: false,
                showColumns: false,
                search:false,
                searchFormVisible:true,
                showExport: false,
                columns: [
                    [
                        {field: 'id', title: 'ID',operate: false},
                        {field: 'car.car_numbers', title: '车牌', operate: 'LIKE'},
                        {field: 'type', title: '图片类别', formatter: Table.api.formatter.flag, searchList: {1:"证件照正面", 2:"证件照反面", 3: "车外观左侧", 4: "车外观右侧", 5: "车外观车头", 6: "车外观车尾", 7: "车内饰主驾驶", 8: "车内饰副驾驶", 9: "车内饰中控", 10: "车内饰后排", 11: "车轮左前", 12: "车轮左后", 13: "车轮右前", 14: "车轮右后", 15: "发动机", 16: "蓄电池"}},
                        {field: 'url', title: '原图',  formatter: Table.api.formatter.image, events: Table.api.events.image,operate: false},
                        {field: 'new_url', title: '待审核图片', formatter: Table.api.formatter.image, events: Table.api.events.image,operate: false},
                        {field: 'status', title: '状态', formatter: Table.api.formatter.flag, searchList: {0:"待审核", 1:"审核通过", 2: "审核不通过"}},
                        {field: 'createtime', title: '添加时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: '更新时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: '通过',
                                    text: '通过',
                                    title: '通过',
                                    confirm: '确认审核通过吗？',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: 'car/image/pass',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status == 0) {
                                            return true;
                                        } else {
                                            return false;
                                        }

                                    },
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                },
                                {
                                    name: '拒绝',
                                    text: '拒绝',
                                    title: '拒绝',
                                    confirm: '确认拒绝吗？',
                                    classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                    url: 'car/image/refuse',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status == 0) {
                                            return true;
                                        } else {
                                            return false;
                                        }

                                    },
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                },
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
